package com.greenet.companytemplate.manager.web;

import com.greenet.companytemplate.manager.entity.TMenu;
import com.greenet.companytemplate.manager.service.ThirdMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/27.
 */

@Controller("third")
public class ThirdController {
    protected Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    private ThirdMenuService thirdMenuService;

    //暂时不用
    @Deprecated
    @RequestMapping("/list/third")
    @ResponseBody
    public List getThirdMenu(@RequestParam("parentId") int pid){
        List list = new ArrayList();
        try {
            list = thirdMenuService.getTidMenu(pid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 将用户添加的三级菜单插入到数据库中
     * @param menu
     * @return
     */
    @RequestMapping("/add/third")
    @ResponseBody
    public int addSecond(TMenu menu,@RequestParam("parentId") int pid){
        logger.info(menu.getName());
        logger.info("父菜单id："+pid);
        menu.setName(menu.getName().trim());
        int thirdId = 0;
        try {
            List<TMenu> list = thirdMenuService.getMenuByName(menu.getName());
            if (list.size()>0) {
                        return -1;
                }
            else {
                thirdId = thirdMenuService.insert(menu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return thirdId;
    }


}
